package xyz.itpath.tools;

import java.util.Random;

/**
 * @author zhaom
 * @time 2021/9/7 - 16:07
 * <p>
 * 编码生成
 */
public class CodeUtil {

    private static final Random RANDOM = new Random();

    /**
     * 生成编码（随机）
     *
     * @param format 编码格式 # 是数字 * 是大写字母 ?是小写字母 .是数字或字母
     * @return
     */
    public static String genCode(String format) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0, j = format.length(); i < j; i++) {
            if ('#' == format.charAt(i)) {
                sb.append(getNumber());
            } else if ('*' == format.charAt(i)) {
                sb.append(getLetter());
            } else if ('?' == format.charAt(i)) {
                sb.append(getLowerLetter());
            } else if ('.' == format.charAt(i)) {
                if (RANDOM.nextInt(10) % 2 == 0) {
                    sb.append(getNumber());
                } else {
                    sb.append(getLetter());
                }
            }
        }
        return sb.toString();
    }

    private static String getLetter() {
        return String.valueOf((char) ('A' + RANDOM.nextInt(26)));
    }

    private static String getLowerLetter() {
        return String.valueOf((char) ('a' + RANDOM.nextInt(26)));
    }

    private static String getNumber() {
        return String.valueOf(RANDOM.nextInt(10));
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            System.out.println(genCode(".*??.."));
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
